from sqlalchemy import Column, Integer, String, DateTime
from plugins.mysql.database import Base, engine
from pydantic import BaseModel
from datetime import datetime
import random



def generate_user_id():
    return random.randint(1000000000, 9999999999)


def generate_random_phone_number():
    second_digit = random.choice(['3', '4', '5', '6', '7', '8', '9'])
    # 随机生成后八位
    last_eight_digits = ''.join(random.choices('0123456789', k=8))
    # 拼接手机号
    phone_number = f"1{second_digit}{last_eight_digits}"
    return phone_number


# 定义名字和姓氏的列表
first_names = [
    "James", "John", "Robert", "Michael", "William",
    "David", "Richard", "Charles", "Joseph", "Thomas",
    "Daniel", "Matthew", "Anthony", "Mark", "Donald"
]
last_names = [
    "Smith", "Johnson", "Williams", "Jones", "Brown",
    "Davis", "Miller", "Wilson", "Moore", "Taylor",
    "Anderson", "Thomas", "Jackson", "White", "Harris"
]
def generate_random_name():
    # 随机选择名字和姓氏
    first_name = random.choice(first_names)
    last_name = random.choice(last_names)

    # 拼接成完整的名字
    full_name = f"{first_name} {last_name}"
    return full_name

# SQLAlchemy 用户模型
class User(Base):
    # 用户信息字段
    __tablename__ = "user"
    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(255), index=True, default=generate_random_name())
    email = Column(String(35), index=True, unique=True)
    phone = Column(String(11), index=True, default=generate_random_phone_number())
    password = Column(String(255), index=True)
    user_id = Column(String(255), index=True, default=generate_user_id())
    add_time = Column(DateTime, default=datetime.now)
    tag = Column(String(255), index=True)

# Pydantic 基类
class UserBase(BaseModel):
    name: str
    email: str
    phone: int
    password: str
    user_id: int
    add_time: datetime
    tag: str

    class Config:
        from_attributes  = True  # 启用 from_attributes  以支持从 ORM 模型转换

# 定义新增用户请求体模型
class UserCreate(BaseModel):
    email: str
    password: str

# 定义登录请求体模型
class UserLogin(BaseModel):
    email: str
    password: str

# 定义登录请求体模型
class EditUser(BaseModel):
    name: str
    email: str
    phone: int
    tag: str

class UserToken(String):
    token: str

class UserResponse(UserBase):
    user_id: str
    name: str
